<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Services\FriendsServices;
use App\Http\Services\UserServices;
use App\Http\Services\MsgServices;
use App\Http\Services\UserBlacklistServices;
use App\Http\Services\UserAgentServices;
use App\Http\Services\SystemServices;

class InnerUserController extends BaseController
{
    protected $user_service;
    protected $friends_services;
    protected $msg_service;
    protected $userBlacklistServices;
    protected $userAgentServices;
    protected $systemService;

    public function __construct()
    {
        $this->user_service = new UserServices();
        $this->friends_services = new FriendsServices();
        $this->msg_service = new MsgServices();
        $this->userBlacklistServices = new UserBlacklistServices();
        $this->userAgentServices = new UserAgentServices();
        $this->systemService = new SystemServices();

    }

    /**
     * 获取用户融云token
     */
    public function getUserRongToken(Request $request)
    {
        $userId = (int) $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $token = $this->user_service->getUserRongToken($userId);
        if(empty($token)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = [
            'user_id' => $userId,
            'rong_token' => $token,
        ];

        Response::returnResult(Hint::SUCCESS, $data);
    }

    //关注用户
    public function post_follow(Request $request){
        $user_id = $request->input("user_id");
        $follow_id = $request->input("follow_id");
        if(empty($user_id) || empty($follow_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $cacheKey = 'USER:FOLLOW:' . $user_id.":".$follow_id;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $ret = null;
        if(!$request->input("action")){
            $ret = $this->friends_services->add_follow($user_id, $follow_id);
        }else{
            $ret = $this->friends_services->del_follow($user_id, $follow_id);
        }

        $data['is_friends']= isset($ret['is_friends']) ? $ret['is_friends'] : false;
        $data['private_msg'] = $ret['private_msg'] ?? '';
        Response::returnResult(Hint::SUCCESS, $data);
    }

    //主播审核
    public function anchorVerify(Request $request){
        $user_id = $request->input("user_id");
        $identity_status = $request->input("identity_status");
        if(empty($user_id) || empty($identity_status)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $cacheKey = 'USER:ANCHORVERIFY:' . $user_id;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        do {
            $userInfo = $this->user_service->getUserData($user_id);
            if($userInfo['identity_status'] == $identity_status) {
                break;
            }

            //用户信息修改
            $res = $this->user_service->anchorVerify($user_id, $identity_status);
            //通过 忙碌中
            if($identity_status == 3) {
                $this->msg_service->anchorVerifyMsg($user_id, '您已通过审核，请通过在线选择按钮选择你的状态');
            }
            //审核拒绝
            if($identity_status == 4) {
                $this->msg_service->anchorVerifyMsg($user_id, '抱歉您这次未通过审核，请您重新提交');
            }
        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    //用户性别重置
    public function genderReset(Request $request){
        $user_id = $request->input("user_id");
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $cacheKey = 'USER:GENDERRESET:' . $user_id;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        do {
            $userInfo = $this->user_service->getUserData($user_id);
            if($userInfo['gender'] == 3) {
                break;
            }

            //用户性别重置
            $res = $this->user_service->genderReset($user_id);

        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    //用户验证码次数重置
    public function smsCodeReset(Request $request){
        $user_id = $request->input("user_id");
        if(empty($user_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $cacheKey = 'USER:SMSCODERESET:' . $user_id;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        do {
            $userInfo = $this->user_service->getUserData($user_id, true);
            if(empty($userInfo)) {
                Response::returnResult(Hint::PARAM_ERROR);
            }
            Redis::DEL('lock:CACHE:MAX:SEND:CODE:'.date("Ymd").':'.$userInfo['phone']);
            Redis::DEL('lock:CACHE:MAX:SEND:CODE:HOUR:'.date("Ymd").':'.$userInfo['phone']);

        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    //手机号验证码重置
    public function phonoCodeReset(Request $request){
        $phone = $request->input("phone");
        if(empty($phone)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $cacheKey = 'USER:PHONECODERESET:' . $phone;
        if (!redis_lock($cacheKey, 1)) {
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        do {
            Redis::DEL('lock:CACHE:MAX:SEND:CODE:'.date("Ymd").':'.$phone);
            Redis::DEL('lock:CACHE:MAX:SEND:CODE:HOUR:'.date("Ymd").':'.$phone);

        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    //解除系统用户黑名单
    public function blackListDelete(Request $request){
        $userId = $request->input("userId");
        $reason = $request->input("reason");
        if(empty($userId) || empty($reason)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        do {
            $this->userBlacklistServices->blackListDelete($userId, $reason);
        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    //新增系统用户黑名单
    public function blackListAdd(Request $request){
        $userId = $request->input("userId");
        if(empty($userId)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        do {
            $this->userBlacklistServices->blackListAdd($userId);
        } while (false);

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 新增代理用户
     */
    public function agentUserAdd(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $rate = (int) $request->input("rate", 0);

        $data = $this->userAgentServices->agentUserAdd($userId, $rate);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 删除代理用户
     */
    public function agentUserDel(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->userAgentServices->agentUserDel($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 编辑代理用户
     */
    public function agentUserEdit(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $rate = (int) $request->input("rate", 0);

        $data = $this->userAgentServices->agentUserEdit($userId, $rate);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //更改用户审核资料状态
    public function changechatfriend(Request $request){
        $id = $request->post('id',0);
        $status = $request->post('status',0);
        if (empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if (empty($status)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $this->user_service->changeChatFriend($id, $status);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 搜索用户
     */
    public function searchUser(Request $request)
    {
        $keywords = $request->input("keywords");
        if (empty($keywords)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $page = (int) $request->get('page', 1);//自1开始
        if($page < 1) $page = 1;
        $page = $page - 1;//真实分页自0开始
        $limit = (int) $request->get('limit', 20);

        $data = $this->user_service->searchUser($keywords, $page, $limit);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 新增代理金币用户
     */
    public function agentGolodUserAdd(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $rate = (int) $request->input("rate", 0);

        $data = $this->userAgentServices->agentGolodUserAdd($userId, $rate);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 删除代理金币用户
     */
    public function agentGolodUserDel(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->userAgentServices->agentGolodUserDel($userId);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 编辑代理金币用户
     */
    public function agentGolodUserEdit(Request $request)
    {
        $userId = $request->input("user_id");
        if (empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $rate = (int) $request->input("rate", 0);

        $data = $this->userAgentServices->agentGolodUserEdit($userId, $rate);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}